package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var lib *MusicManager
var id int = 1
var ctrl, signal chan int

func handleLibCommands(tokens []string) {
	switch tokens[1] {
	case "list":
		for i := 0; i<lib.Len() ;i++  {
			e, _ := lib.Get(i)
			fmt.Println(i+1, ":", e.Name, e.Artist, e.Source, e.Type)
		}
	case "add":{
		if len(tokens) == 6 {
			id ++
			lib.Add(&MusicEntry{string(id),tokens[2], tokens[3], tokens[4], tokens[5]})
		} else {
			fmt.Println("命令长度不够")
		}
	}
	case "remove":
		if len(tokens) == 3 {
			l, _ := strconv.Atoi(tokens[2])
			lib.Remove(l)
		} else {
			fmt.Println("命令长度不够")
		}


	}
}

func handlePlayCommand(tokens []string) {
	if len(tokens) != 2 {
		fmt.Println("命令错误")
		return
	}

	e := lib.Find(tokens[1])
	if e == nil {
		fmt.Println("没找到这个歌")
		return
	}

	Play(e.Source, e.Type)

}

func main() {

	lib = &MusicManager{}

	r := bufio.NewReader(os.Stdin)

	for {
		fmt.Println("Enter Command->")
		rawLine, _, _ := r.ReadLine()
		line := string(rawLine)

		if line == "q" || line == "e" {
			break
		}

		tokens := strings.Split(line, " ")
		if tokens[0] == "lib" {
			handleLibCommands(tokens)
		} else if tokens[0] == "play" {
			handlePlayCommand(tokens)
		} else {
			fmt.Println("无法理解命令:", tokens[0])
		}
	}

}